import express from "express";
import "express-namespace";
import chalk from "chalk";
import path from "node:path";
// import wsInit from "./utils/ws-helper.js";

import { registRouter } from "./router/index.js";
import { registRouterIo } from "./router-io/index.js";
import "./utils/gdata.js";
import nunjucks from "nunjucks";
import cors from "cors";

// 创建express服务实例 启动http服务
const app = express();
nunjucks.configure("app/views", {
  autoescape: true,
  express: app,
  tags: { // 修改一下 避免和vue的{{xx}}冲突
    variableStart: '<%',
    variableEnd: '%>'
  }
});
app.use(cors());
app.use(express.json()); // 解析post参数 req.body获取
app.use(express.urlencoded({ extended: true })); // 解析from参数
app.use(express.static(path.resolve("app", "views"))); //配置静态资源目录
await registRouter(app); // 注册路由



const server = app.listen("3002", () => {
  console.log(chalk.green(chalk.bold("启动成功 ") + "http://localhost:3002"));
});
registRouterIo(server); // 注册ws路由
